/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.entitydataconvertor.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import lombok.var;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;

import java.util.ArrayList;
import java.util.Arrays;

public class CheckChildGenerator extends ClassMethodGenerator {

  private static final String ProName = "childCode";
  private GspViewObject vmObject;

  public CheckChildGenerator(GspViewObject vmObject) {
    this.vmObject = vmObject;
  }


  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new ArrayList(Arrays.asList(
        new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PROTECTED_KEYWORD}));
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return "checkChildCode";
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ParameterInfo paramInfo = new ParameterInfo();
    paramInfo.setParamType(new TypeInfo("String"));
    paramInfo.setParamName(ProName);
    return new ArrayList<ParameterInfo>(
        Arrays.asList(new ParameterInfo[]{paramInfo}));
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("boolean");
  }

  @Override
  protected Block buildMethodBody() {
    var block = ast.newBlock();
    var childObjects = vmObject.getContainChildObjects();
    if (childObjects != null && childObjects.size() > 0) {
      for (var childNode : childObjects) {
        block.statements().add(getIfStatement(childNode.getCode()));
      }
    }

    block.statements().add(getReturnFalseStatement());

    return block;
  }

  private Statement getIfStatement(String nodeCode) {
    var ifStatement = ast.newIfStatement();

    var condition = ast.newMethodInvocation();
    condition.setExpression(ast.newSimpleName(ProName));
    condition.setName(ast.newSimpleName("equals"));

    var value = ast.newStringLiteral();
    value.setLiteralValue(nodeCode);
    condition.arguments().add(value);

    ifStatement.setExpression(condition);

    var thenBlock = ast.newBlock();
    thenBlock.statements().add(getReturnTrueStatement());
    ifStatement.setThenStatement(thenBlock);

    return ifStatement;
  }

  private ReturnStatement getReturnTrueStatement() {
    return getReturnStatement(true);
  }

  private ReturnStatement getReturnFalseStatement() {
    return getReturnStatement(false);
  }

  private ReturnStatement getReturnStatement(boolean value){
    var returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newBooleanLiteral(value));
    return returnStatement;
  }
}
